/*
 *  SequenceElement.cpp
 *  SoundController0002
 *
 *  Created by jiro on 11/09/14.
 *  Copyright 2011 jiro music. All rights reserved.
 *
 */

#include "SequenceElement.h"

#include "../HarmonicSystem/HarmonicSystem.h"
#include "../0039/Theory0039.h"
#include "../../sc/AudioRecorder.h"
#include "../../sc/SoundGenerator.h"

using namespace ::sc::hs;

namespace sc
{
namespace system0039
{

	SequenceElement::SequenceElement()
	{}

	SequenceElement::SequenceElement( sc::Note::MidiNumber number0, double harmonics0, sc::Note::MidiNumber number1, double harmonics1, 
									  double dynamics, int repeatCount )
		: frequencies(),
		  dynamics( dynamics ),
		  repeatCount( repeatCount )
	{
		frequencies[ 0 ] =  harmonics0 * WellTemperament::GetFrequency( number0 );
		frequencies[ 1 ] =  harmonics1 * WellTemperament::GetFrequency( number1 );
	}

	SequenceElement::SequenceElement( sc::Note::MidiNumber number0, double harmonics0, sc::Note::MidiNumber number1, double harmonics1, double dynamics )
		: frequencies(),
		  dynamics( dynamics ),
		  repeatCount( 1 )
	{
		frequencies[ 0 ] =  harmonics0 * WellTemperament::GetFrequency( number0 );
		frequencies[ 1 ] =  harmonics1 * WellTemperament::GetFrequency( number1 );
	}

	SequenceElement::SequenceElement( double frequency0, double frequency1, double dynamics )
		: frequencies(),
		  dynamics( dynamics ),
		  repeatCount( 1 )
	{
		frequencies[ 0 ] = frequency0;
		frequencies[ 1 ] = frequency1;
	}

	void SequenceElement::SetRepeatCount( int count )
	{
		repeatCount = count;
	}

	double SequenceElement::GetFrequency( int index ) const
	{
		return frequencies[index ];
	}

	double SequenceElement::GetDynamics() const
	{
		return dynamics;
	}

	int SequenceElement::GetRepeatCount() const
	{
		return repeatCount;
	}

	double SequenceElement::GetDuration( bool isRepeat ) const
	{
		double howlFreq = fabs( frequencies[ 0 ] - frequencies[ 1 ] );
		double duration = 1.0 / howlFreq;
		if ( isRepeat )
		{
			duration *= repeatCount;
		}
		return duration;
	}

	::sc::hs::Chord SequenceElement::ToChord() const
	{
		::sc::hs::Chord chord;
		chord.push_back( Sound( frequencies[ 0 ], dynamics ) );
		chord.push_back( Sound( frequencies[ 1 ], dynamics ) );
		return chord;
	}

}
}